
import moduleview from '../models/movie'
import detialHtml from '../views/detial.art'
import detialmovieHtml from '../views/detial-movie.art'
import detialtimeHtml from '../views/detialtime.art'
import detialcinemaHtml from '../views/detialcinema.art'


const moment = require('moment');
class detial {
constructor(){
  this.choosedate=0,
  this.id=0
}
  renderer(list) {
    let html = detialmovieHtml({
      list
    })
    $('.detial-box').html(html)
  }


  async  render() {
    let list = []
  
    let detialhtml = detialHtml({
    })

    $('#movie-root').html(detialhtml)
  
    let  id=this.id=location.hash.substring(10)
    let result = await moduleview.detial(id)
    list.push(result.detailMovie)
    this.renderer(list)
    let now = moment().format();
    let day = now.substr(0, 10)

    let data = await moduleview.detialcinema({
      movieId: id,
      day: `${day}`,
      offset: 0,
      limit: 20,
      districtId: -1,
      lineId: -1,
      hallType: -1,
      brandId: -1,
      serviceId: -1,
      areaId: -1,
      stationId: -1,
      updateShowDay: true,
      reqId: 1571408427631,
      cityId: 1,
    })
   let date=data.showDays.dates
   // console.log(data)
 this.timerender (date)
 this.setActiveClass()
 // console.log(data.cinemas)
    this.cinemarender(data.cinemas)
  }

   timerender(date){
  let html=detialtimeHtml ({
   date
   })
   $('.date ul').html(html)
}


cinemarender(result){
  let html=detialcinemaHtml ({
   list:result
    })
    $('.detial-cinema ul').html(html)
}

 setActiveClass(){
   let that=this
  $('.date ul li:first-child').addClass('active-date')
  $('.date ul li').on('tap',async function(){
    $(this).addClass('active-date').siblings().removeClass('active-date') 
     that.choosedate = $(this).html()
     let data = await moduleview.detialcinema({
      movieId: that.id,
      day: `${that.choosedate}`,
      offset: 0,
      limit: 20,
      districtId: -1,
      lineId: -1,
      hallType: -1,
      brandId: -1,
      serviceId: -1,
      areaId: -1,
      stationId: -1,
      updateShowDay: true,
      reqId: 1571408427631,
      cityId: 1,
    }) 
    that.cinemarender(data.cinemas)
     console.log( this.choosedate)
  }) 
 
  }


  handleurl(result, url) {
    let re = /w.h/g
    let len = result.coming.length
    for (let i = 0; i < len; i++) {
      result.coming[i].img = result.coming[i].img.replace(re, url)
    }
    let list = this.list = result.coming
    return list

  }
}

export default new detial()